package cc.orange.mainView;

import androidx.databinding.DataBindingUtil;
import cc.orange.BuildConfig;
import cc.orange.utils.ClicksSpans;

import cc.orange.databinding.ActivityLonginWxBinding;

import cc.orange.R;
import cc.orange.adapter.SplashAdapter;
import cc.orange.base.BaseActivity;
import cc.orange.entity.WxInfoEntity;
import cc.orange.utils.ScollLinearLayoutManager;
import cc.orange.utils.StatusBarUtilView;
import cc.orange.utils.WxLogUtil;
import cc.orange.utils.ZXToastUtil;
import com.tendcloud.tenddata.TalkingDataSDK;

import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.view.DisplayCutout;
import android.view.View;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class LonginWxActivity extends BaseActivity {

    private ActivityLonginWxBinding binding;
    private int type = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_longin_wx);

        binding.msgaRel5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //选择
        binding.loginImg2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(type == 0){
                    type = 1;
                    binding.loginImg1.setVisibility(View.VISIBLE);
                    binding.loginImg3.setVisibility(View.INVISIBLE);
                }else{
                    type = 0;
                    binding.loginImg1.setVisibility(View.INVISIBLE);
                    binding.loginImg3.setVisibility(View.VISIBLE);
                }
            }
        });

        binding.loginClick1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });


        //登录
        binding.loginClick3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type == 0) {
                    ZXToastUtil.showToast("请先同意相关协议");
                    return;
                }
                WxLogUtil.loginWX();
                finish();
//                WxInfoEntity ss = new WxInfoEntity();
//                ss.setHeadimgurl("http://182.92.160.135:90/kefu.jpg");
//                ss.setNickname("测试账号");
//                ss.setOpenid("123456"+System.currentTimeMillis());
//                ss.setSex(0);
//                EventBus.getDefault().post(ss);
            }
        });
        binding.loginClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type == 0) {
                    ZXToastUtil.showToast("请先同意相关协议");
                    return;
                }
                WxLogUtil.loginWX();
                finish();
//                WxInfoEntity ss = new WxInfoEntity();
//                ss.setHeadimgurl("http://182.92.160.135:90/kefu.jpg");
//                ss.setNickname("测试账号");
//                ss.setOpenid("123456"+System.currentTimeMillis());
//                ss.setSex(0);
//                EventBus.getDefault().post(ss);
            }
        });

        String content = "现有<font color=\"#B05FFC\">200万</font>人 等你相遇";
        binding.loginWxText1.setText(Html.fromHtml(content));

        String s = "<font color=\"#666666\">登录即同意" +
                "<font ><a href= "+BuildConfig.WEBS_URL1+">《用户服务协议》</a ></font>" +
                "和<font ><a href="+BuildConfig.WEBS_URL2+">《隐私政策》</a ></font></font>";
        binding.loginText1.setText(getClickableHtml(this,s));
        //这一句很重要，否则ClickableSpan内的onClick方法将无法触发！！ color="#F48337"
        binding.loginText1.setMovementMethod(LinkMovementMethod.getInstance());

        EventBus.getDefault().register(this);

    }

    private DisplayCutout cutoutDisp = null;
    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        StatusBarUtilView.transparencyBar(LonginWxActivity.this);
        StatusBarUtilView.setLightStatusBar(LonginWxActivity.this, true, true);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getLogisnMsg(WxInfoEntity wxInfoEntity) {
//        ARouter.getInstance().build(RouterRath.MainActivity)
//                .navigation();
        finish();
    }

    /**
     * 格式化超链接文本内容并设置点击处理
     * */
    public static CharSequence getClickableHtml(Context context, String html) {
        Spanned spannedHtml = Html.fromHtml(html);
        SpannableStringBuilder clickableHtmlBuilder = new SpannableStringBuilder(spannedHtml);
        URLSpan[] urls = clickableHtmlBuilder.getSpans(0, spannedHtml.length(), URLSpan.class);
        for(final URLSpan span : urls) {
            setLinkClickable(context,clickableHtmlBuilder, span);
        }
        return clickableHtmlBuilder;
    }

    /**
     * 设置点击超链接对应的处理内容
     * */
    public static void setLinkClickable(final Context context, final SpannableStringBuilder clickableHtmlBuilder, final URLSpan urlSpan) {
        int start = clickableHtmlBuilder.getSpanStart(urlSpan);
        int end = clickableHtmlBuilder.getSpanEnd(urlSpan);
        int flags = clickableHtmlBuilder.getSpanFlags(urlSpan);
        ClicksSpans clickableSpan = new ClicksSpans() {
            @Override
            public void onClick(View view) {
                if(urlSpan.getURL().contains("yonghu")){//隐私政策
                    WebViewActivity.openH5Activity(context,"隐私政策", BuildConfig.WEBS_URL2);
                }else if(urlSpan.getURL().contains("xieyi")){
                    WebViewActivity.openH5Activity(context,"服务协议",BuildConfig.WEBS_URL1);
                }
            }
        };
        //实际使用过程中，可根据需要清空之前的span，防止出现重复调用的情况
        clickableHtmlBuilder.setSpan(clickableSpan, start, end, flags);
    }


    @Override
    protected void onResume() {
        super.onResume();
        TalkingDataSDK.onPageBegin(this, getLocalClassName());
    }

    @Override
    protected void onPause() {
        super.onPause();
        TalkingDataSDK.onPageEnd(this, getLocalClassName());
    }
}